﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-13 12:49:27
// 负责人：lvsc
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
using OF.Component.DTO;

namespace OF.Component.DAL
{
	
	/// <summary>
	///办事指南 数据访问层
	/// </summary>	
	internal partial class Guidance
	{		
    
        /// <summary>
    	/// 根据断言条件获取单个办事指南
    	/// </summary>
        public GuidanceInfo QuerySingle(Expression<Func<GuidanceInfo, bool>> predicate)
	    {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }
        
        /// <summary>
    	/// 根据断言条件获获取办事指南列表
    	/// </summary>
	    public IList<GuidanceInfo> QueryList(Expression<Func<GuidanceInfo, bool>> predicate)
	    {
	        return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
	    }

        /// <summary>
        /// 根据城市和业务获取办事指南
        /// </summary>
        /// <param name="citId"></param>
        /// <param name="bizTypeId"></param>
        /// <returns></returns>
        public GuidanceInfo GetGuidances(int citId, int bizTypeId)
        {
            GuidanceInfo info=null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM Guidance WITH(NOLOCK) WHERE CityID=@CityID AND BizTypeID=@BizTypeID AND IsActive=1 ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@CityID", DbType.Int32, citId);
            db.AddInParameter(dbc, "@BizTypeID", DbType.Int32, bizTypeId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info=GetByReader(dr);
                }
            }
            return info;
        }

        /// <summary>
        /// 查询分页列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<GuidanceDto> SearchList(GuidanceCondition condition)
        {
            IList<GuidanceDto> list = new List<GuidanceDto>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("[p_Guidance_search]");
            //设置参数
            db.AddInParameter(dbc, "@GuidanceID", DbType.Int32, condition.GuidanceID);
            db.AddInParameter(dbc, "@ProvinceId", DbType.Int32, condition.ProvinceId);
            db.AddInParameter(dbc, "@CityId", DbType.Int32, condition.CityId);
            db.AddInParameter(dbc, "@MasterBizTypeId", DbType.Int32, condition.MasterBizTypeId);
            db.AddInParameter(dbc, "@BizTypeId", DbType.Int32, condition.BizTypeId);
            db.AddInParameter(dbc, "@StartDateTime", DbType.String, condition.StartDateTime);
            db.AddInParameter(dbc, "@EndDateTime", DbType.String, condition.EndDateTime);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, condition.page);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, condition.rows);
            db.AddOutParameter(dbc, "@OutCount", DbType.Int32, 4);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    list.Add(SQLServerProvider.Create<GuidanceDto>(dr));
                }
            }
            condition.total = (int)db.GetParameterValue(dbc, "@OutCount");
            return list;
        }

    }

}